package card_discount

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"context"
)

// ListItemCategoryConfigurationPtrsByCardDiscountID 获取卡折扣到商品品类配置数组
func (s *CardDiscountEntityServiceImp) ListItemCategoryConfigurationPtrsByCardDiscountID(ctx context.Context, q *sqlc.Queries, couponID int64) (arr param.ItemCategoryConfigurationPtrArray, err error) {
	couponItemCategories, err := q.ListNormalCardDiscountItemCategoriesByCardDiscountID(ctx, couponID)
	if err != nil {
		return
	}
	for _, couponItemCategory := range couponItemCategories {
		arr = append(arr, &param.ItemCategoryConfiguration{
			ID:   couponItemCategory.ItemCategoryID,
			Name: couponItemCategory.ItemCategoryName,
		})
	}
	return
}

// GetMapOfCardDiscountIDToItemCategoryConfigurationPtrsByCardDiscountIDs 获取卡折扣到商品品类配置的映射
func (s *CardDiscountEntityServiceImp) GetMapOfCardDiscountIDToItemCategoryConfigurationPtrsByCardDiscountIDs(ctx context.Context, q *sqlc.Queries, cardDiscountIDs []int64) (m param.EntityIDToItemCategoryConfigurationPtrsMap, err error) {
	m = make(param.EntityIDToItemCategoryConfigurationPtrsMap)
	if len(cardDiscountIDs) == 0 {
		return
	}
	cardDiscountItemCategories, err := q.ListNormalCardDiscountItemCategoriesByCardDiscountIDs(ctx, cardDiscountIDs)
	if err != nil {
		return
	}
	for _, cardDiscountItemCategory := range cardDiscountItemCategories {
		cardDiscountID := cardDiscountItemCategory.CardDiscountID
		valPtr := &param.ItemCategoryConfiguration{
			ID:   cardDiscountItemCategory.ItemCategoryID,
			Name: cardDiscountItemCategory.ItemCategoryName,
		}
		if val, ok := m[cardDiscountID]; ok {
			val = append(val, valPtr)
			m[cardDiscountID] = val
		} else {
			m[cardDiscountID] = []*param.ItemCategoryConfiguration{valPtr}
		}
	}
	return
}

// GetMapOfCardDiscountIDToItemCategoryConfigurationPtrsByCardDiscountPtrs 获取卡折扣到商品品类配置的映射
func (s *CardDiscountEntityServiceImp) GetMapOfCardDiscountIDToItemCategoryConfigurationPtrsByCardDiscountPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.CardDiscount) (m param.EntityIDToItemCategoryConfigurationPtrsMap, err error) {
	return s.GetMapOfCardDiscountIDToItemCategoryConfigurationPtrsByCardDiscountIDs(ctx, q, s.ConvToCardDiscountIDs(ptrs))
}

// GetMapOfCardDiscountIDToItemCategoryConfigurationPtrsByListCardDiscountWithStatRowPtrs 获取卡折扣到商品品类配置的映射
func (s *CardDiscountEntityServiceImp) GetMapOfCardDiscountIDToItemCategoryConfigurationPtrsByListCardDiscountWithStatRowPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.ListCardDiscountWithStatsRow) (m param.EntityIDToItemCategoryConfigurationPtrsMap, err error) {
	return s.GetMapOfCardDiscountIDToItemCategoryConfigurationPtrsByCardDiscountIDs(ctx, q, s.ConvToCardDiscountIDs(ptrs))
}
